<?php

namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Iot\V20180120\Iot;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Tea;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Iot\V20180120\Models\QueryProductRequest;
use AlibabaCloud\SDK\Iot\V20180120\Models\CreateProductRequest;
use AlibabaCloud\SDK\Iot\V20180120\Models\UpdateProductRequest;
use AlibabaCloud\SDK\Iot\V20180120\Models\DeleteProductRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class Sample 
{
    /**
     * 使用AK&SK初始化Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([]);
        // 您的AccessKey ID。
        $config->accessKeyId = $accessKeyId;
        // 您的AccessKey Secret。
        $config->accessKeySecret = $accessKeySecret;
        // 您的可用区ID。
        $config->endpoint = "iot.cn-shanghai.aliyuncs.com";
        return new Iot($config);
    }
    /**
     * QueryProduct：产品管理类API，用于查询指定产品的详细信息
     * @param string $accessKey 
     * @param string $accessKeySecret 
     * @param string $productKey 产品秘钥
     * @param string $iotInstanceId 示例Id
     * @return array $response 产品信息
     */
    public function queryProduct($accessKey,$accessKeySecret,$productKey,$iotInstanceId)
    {
        $client = self::createClient("${accessKey}", "${accessKeySecret}");
        $request = new QueryProductRequest([
            "iotInstanceId" => "${iotInstanceId}",
            "productKey" => "${productKey}",
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            $response=$client->queryProductWithOptions($request, $runtime);
            echo(json_encode(Utils::toJSONString(Tea::merge($response))));
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            echo(json_encode($error->message));
        }
    }

    public function createProduct($accessKey,$accessKeySecret,$productName,$iotInstanceId,$nodeType,$description)
    {
        $client = self::createClient("${accessKey}", "${accessKeySecret}");
        $request = new CreateProductRequest([
            "nodeType" => $nodeType,
            "productName" => $productName,
            "iotInstanceId"=> $iotInstanceId,
            "description" => $description
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            $response=$client->createProductWithOptions($request, $runtime);
            echo(json_encode(Utils::toJSONString(Tea::merge($response))));
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            echo(json_encode($error->message));
        }
    }

    public function updateProduct($accessKey,$accessKeySecret,$productKey,$productName,$iotInstanceId,$description)
    {
        $client = self::createClient("${accessKey}", "${accessKeySecret}");
        $request = new UpdateProductRequest([
            "productKey" => $productKey,
            "productName" => $productName,
            "iotInstanceId"=> $iotInstanceId,
            "description" => $description
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            $response=$client->updateProductWithOptions($request, $runtime);
            echo(json_encode(Utils::toJSONString(Tea::merge($response))));
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            echo(json_encode($error->message));
        }
    }

    public function deleteProduct($accessKey,$accessKeySecret,$productKey,$iotInstanceId)
    {
        $client = self::createClient("${accessKey}", "${accessKeySecret}");
        $request = new DeleteProductRequest([
            "productKey" => $productKey,
            "iotInstanceId"=> $iotInstanceId,
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            $response=$client->deleteProductWithOptions($request, $runtime);
            echo(json_encode(Utils::toJSONString(Tea::merge($response))));
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            echo(json_encode($error->message));
        }
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
Sample::createProduct("your accessKey","your accessKeySecrete","your productName","your iotInstanceId","your nodeType","your description");
Sample::queryProduct("your accessKey","your accessKeySecrete","your productKey","your iotInstanceId");
Sample::updateProduct("your accessKey","your accessKeySecrete","your productKey","your productName","your iotInstanceId","your description");
Sample::deleteProduct("your accessKey","your accessKeySecrete","your productKey","your iotInstanceId");